package wb.is.besti.edu.cd.javalibgame;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by asus on 2017/12/17.
 */

public class AddGameView extends LinearLayout{
    private AddCard[][] cardsMap = new AddCard[Config.LINES][Config.LINES];
    public AddGameView(@NonNull Context context) {
        super(context);
        initGameView();
    }

    public AddGameView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initGameView();
    }
    private void initGameView(){
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(0xff48D1CC);
        setOnTouchListener(new OnTouchListener() {
            private int startX, startY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX()/Config.CARD_WIDTH;
                        startY = (int) event.getY()/Config.CARD_WIDTH;
                        cardsMap[startX][startY].setNum(cardsMap[startX][startY].getNum()+1);
                        starAction(startX,startY);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
    public void starAction(int x,int y){
        int nums = 0;
        AddCard up = cardsMap[x][y-1];
        AddCard down = cardsMap[x][y+1];
        AddCard left = cardsMap[x-1][y];
        AddCard right = cardsMap[x+1][y];
        if (up.getNum()==cardsMap[x][y].getNum()){
            nums++;
        }
        if (down.getNum()==cardsMap[x][y].getNum()){
            nums++;
        }
        if (left.getNum()==cardsMap[x][y].getNum()){
            nums++;
        }
        if (right.getNum()==cardsMap[x][y].getNum()){
            nums++;
        }
        if (nums==4){
            TextAddGame.getTextAddGame().getAnimLayer().createMoveAnim(up,cardsMap[x][y],x,x,y-1,y);
            TextAddGame.getTextAddGame().getAnimLayer().createMoveAnim(down,cardsMap[x][y],x,x,y+1,y);
            TextAddGame.getTextAddGame().getAnimLayer().createMoveAnim(left,cardsMap[x][y],x-1,x,y,y);
            TextAddGame.getTextAddGame().getAnimLayer().createMoveAnim(right,cardsMap[x][y],x+1,x,y,y);
            cardsMap[x][y].setNum(cardsMap[x][y].getNum()*4);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Config.CARD_WIDTH = (Math.min(w, h) - 10) / Config.LINES;
        addCards(Config.CARD_WIDTH, Config.CARD_WIDTH);
        startgame();
    }
    private void addCards(int cardWidth, int cardHeight){
        AddCard card;
        LinearLayout line;
        LinearLayout.LayoutParams lineLp;
        for (int y = 0; y < Config.LINES; y++) {
            line = new LinearLayout(getContext());
            lineLp = new LinearLayout.LayoutParams(-1, cardHeight);
            addView(line, lineLp);
            for (int x = 0; x < Config.LINES; x++) {
                card = new AddCard(getContext());
                line.addView(card, cardWidth, cardHeight);
                cardsMap[x][y] = card;
            }
        }
    }

    public void startgame(){
        for (int y = 0; y < Config.LINES; y++) {
            for (int x = 0; x < Config.LINES; x++) {
                cardsMap[x][y].setNum(1);
            }
        }
    }
}
